﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.IO;
using System.Net;
using System.Configuration;
using System.Net.Mail;
using Esav.ExceptionManagement;


namespace FTPServiceWHR
{
    class FTPCopy
    {

        #region Properties
        protected internal String _Separador;
        public String Separador
        {
            get { return _Separador; }
            set { _Separador = value; }
        }

        protected internal String _PathFTPServerFrom;
        public String PathFTPServerFrom
        {
            get { return _PathFTPServerFrom; }
            set { _PathFTPServerFrom = value; }
        }

        protected internal String _UserNameFrom;
        public String UserNameFrom
        {
            get { return _UserNameFrom; }
            set { _UserNameFrom = value; }
        }


        protected internal String _PasswordFrom;
        public String PasswordFrom
        {
            get { return _PasswordFrom; }
            set { _PasswordFrom = value; }
        }


        protected internal String _NameFileBackup;
        public String NameFileBackup
        {
            get { return _NameFileBackup; }
            set { _NameFileBackup = value; }
        }

        protected internal String _PathFTPServerTo;
        public String PathFTPServerTo
        {
            get { return _PathFTPServerTo; }
            set { _PathFTPServerTo = value; }
        }
        protected internal String _UserNameTo;
        public String UserNameTo
        {
            get { return _UserNameTo; }
            set { _UserNameTo = value; }
        }
        protected internal String _PasswordTo;
        public String PasswordTo
        {
            get { return _PasswordTo; }
            set { _PasswordTo = value; }
        }


        #endregion

        public void CopyFTP()
        {
            {

                this._Separador = ConfigurationSettings.AppSettings["Separador"].ToString();
                this._PathFTPServerFrom = ConfigurationSettings.AppSettings["PathFTPServerFrom"].ToString();
                this._UserNameFrom = ConfigurationSettings.AppSettings["UserNameFrom"].ToString();
                this._PasswordFrom = ConfigurationSettings.AppSettings["PasswordFrom"].ToString();
                this._NameFileBackup = ConfigurationSettings.AppSettings["NameFileBackup"].ToString();
                this._PathFTPServerTo = ConfigurationSettings.AppSettings["PathFTPServerTo"].ToString();
                this._UserNameTo = ConfigurationSettings.AppSettings["UserNameTo"].ToString();
                this._PasswordTo = ConfigurationSettings.AppSettings["PasswordTo"].ToString();



                String sNameFiles = null;
                String strFTPRequestFromBackup = String.Empty;

                StreamReader srFiles = default(StreamReader);
                FtpWebRequest oFTPRequestTo = default(FtpWebRequest);
                FtpWebRequest oFTPRequestFrom = default(FtpWebRequest);
                FtpWebRequest oFTPRequestFromBackup = default(FtpWebRequest);
                FtpWebResponse oFtpWebResponse = default(FtpWebResponse);
                WebClient oWebClient = new WebClient();
                Stream stGetStream = default(Stream);

                byte[] bFTPFile = null;

                try
                {
                    oFTPRequestFrom = (FtpWebRequest)FtpWebRequest.Create(new Uri(this.PathFTPServerFrom));
                    oFTPRequestFrom.Credentials = new NetworkCredential(this.UserNameFrom, this.PasswordFrom);
                    oFTPRequestFrom.Method = WebRequestMethods.Ftp.ListDirectory;
                    //srFiles = Directory.GetFiles(this.PathFTPServerFrom);
                 //   srFiles = new StreamReader(this.PathFTPServerFrom);
                  //  srFiles = new StreamReader(oFTPRequestFrom.GetResponse().GetResponseStream()); 
                
                    sNameFiles = srFiles.ReadLine();

                    while ((sNameFiles != null))
                    {

                        if (sNameFiles != this.NameFileBackup & sNameFiles.IndexOf(".", StringComparison.InvariantCultureIgnoreCase) != -1)
                        {

                            //Get Files 
                            oWebClient.Credentials = new NetworkCredential(this.UserNameFrom, this.PasswordFrom);
                            bFTPFile = oWebClient.DownloadData(this.PathFTPServerFrom + sNameFiles);

                            //Copy Files Backup 
                            strFTPRequestFromBackup = this.PathFTPServerFrom + this.NameFileBackup + "/" + sNameFiles;
                            oFTPRequestFromBackup = (FtpWebRequest)WebRequest.Create(strFTPRequestFromBackup);
                            oFTPRequestFromBackup.Credentials = new NetworkCredential(this.UserNameFrom, this.PasswordFrom);
                            oFTPRequestFromBackup.Method = WebRequestMethods.Ftp.UploadFile;
                            oFTPRequestFromBackup.ContentLength = bFTPFile.Length;
                            stGetStream = oFTPRequestFromBackup.GetRequestStream();
                            stGetStream.Write(bFTPFile, 0, bFTPFile.Length);
                            stGetStream.Close();

                            //Copy Files 
                            oFTPRequestTo = (FtpWebRequest)WebRequest.Create(this.PathFTPServerTo + sNameFiles);
                            oFTPRequestTo.Credentials = new NetworkCredential(this.UserNameTo, this.PasswordTo);
                            oFTPRequestTo.Method = WebRequestMethods.Ftp.UploadFile;
                            oFTPRequestTo.ContentLength = bFTPFile.Length;
                            stGetStream = oFTPRequestTo.GetRequestStream();
                            stGetStream.Write(bFTPFile, 0, bFTPFile.Length);
                            stGetStream.Close();

                            //Delete files from Original Directory 
                            //oFTPRequestFrom = (FtpWebRequest)WebRequest.Create(this.PathFTPServerFrom + sNameFiles);
                            //oFTPRequestFrom.Credentials = new NetworkCredential(this.UserNameFrom, this.PasswordFrom);
                            //oFTPRequestFrom.Method = WebRequestMethods.Ftp.DeleteFile;
                            //oFtpWebResponse = (FtpWebResponse)oFTPRequestFrom.GetResponse();
                            //oFtpWebResponse.Close();
                        }

                        sNameFiles = srFiles.ReadLine();
                    }
                    srFiles.Close();
                    srFiles = null;
                    //oFTPRequestFrom = null;
                }
                catch (Exception ex)
                {
                   Esav.ExceptionManagement.ExceptionManager.Publish(ex);
                }
            }

        }
    }
}
